<div class="stepswithdraw">
    <div class="substeps">
        <div class="substepitmgray1 substepitmblue1">
            <b>01</b> 提交
        </div>
        <div class="substepitmgray2">
            <b>02</b> 审核
        </div>
        <div class="substepitmgray2">
            <b>03</b> 出款
        </div>
        <div class="substepitmgray2">
            <b>04</b> 到账
        </div>
        <div class="clear"></div>
    </div>
    <div class="line2"></div>
</div>
<div id="withdrawal4" class="subcontent2" style="display: block;">
    <div class="addbankcard" id="withdraw-form">
        <div class="formpanel margintop20">
            <div class="wd-row">
                <div class="wd-col1">提款金额：</div>
                <div class="wd-col2">
                    <input id="drawamount" type="text" class="textfield1">
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row">
                <div class="wd-col1">银行卡号：</div>
                <div class="wd-col2">
                    <div class="radio-bank-2">
                        <div class="radio">
                            <input type="radio" name="bank" value="" checked="checked">
                        </div>
                        <div class="radio-label">
                            <span id="bankName" class="name"></span> 
                            <span>尾号：</span><span id="lastCard"></span>
                        </div>
                    </div>
                    <a id="rebindBtn" href="#" style="line-height: 34px;margin-left: 10px;">重新绑定</a>
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row">
                <div class="wd-col1">提款密码：</div>
                <div class="wd-col2">
                    <input id="drawcode1" type="password" class="textfield1">
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row" style="text-align: center;">
                <input type="button" class="c-button" value="提交">
            </div>
        </div>
    </div>

    <div class="addbankcard hide" id="bankcard-form">
        <div id="card_h" class="hide">
            <div class="logo-th-2"></div>
            <p class="bigtxt aligncenter gray">添加一张银行卡开始提款吧</p>
            <div class="margintop40 aligncenter">
                <img src="images/bank/addbankcard.jpg" width="214" height="112">
            </div>
        </div>
        <div id="card_s" class="formpanel margintop20 hide" style="display: block;">
            <div class="wd-row">
                <div class="wd-col1">持卡人姓名：</div>
                <div class="wd-col2">
                    <span id="fullName"></span>
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row">
                <div class="wd-col1">选择银行：</div>
                <div class="wd-col2">
                    <select id="bankCode" class="selectbox gray">
                    </select>
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row">
                <div class="wd-col1">银行卡号：</div>
                <div class="wd-col2">
                    <input type="text" id="cardId" class="textfield1">
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row">
                <div class="wd-col1">开户网点：</div>
                <div class="wd-col2">
                    <input type="text" id="bankaddress" class="textfield1">
                </div>
                <div class="clear"></div>
            </div>
            <div class="wd-row" style="text-align: center;">
                <input type="button" class="c-button" value="提交">
            </div>
        </div>
    </div>
</div>

<script>
    // 如果用户没有绑定银行卡号，则显示添加银行卡的界面
    var bank = context.user.user.bank;
    if (bank) bank = JSON.parse(bank);
    if (Util.isEmpty(bank) || !bank.account) {
        $("#withdraw-form").hide();
        $("#bankcard-form").show();
        $("#card_h").show();
        $("#card_s").hide();
    }
    else {
        $("#withdraw-form").show();
        $("#bankcard-form").hide();
        $("#card_h").hide();
    }
    
    $("#card_h img").click(function() {
        // 切换到添加银行的页面
        $("#card_h").hide();
        $("#card_s").show();
    });
    $("#rebindBtn").click(function() {
        $("#withdraw-form").hide();
        $("#bankcard-form").show();
        $("#card_h").hide();
        $("#card_s").show();
    })

    // 填充银行列表
    Util.fillBanks($("#bankCode"));

    // 填充账户相关信息
    if (!Util.isEmpty(bank) && bank.account) {
        $("#bankName").html(bank.bank || '');
        $("#lastCard").html(bank.account.substring(bank.account.length - 4));
    }
    $("#fullName").html(context.user.user.name || '');

    // 绑定银行卡号的处理
    $("#bankcard-form .c-button").click(function() {
        var bank_account = $("#cardId").val().trim();
        if (!bank_account) {
            alert2("请输入银行卡号", true);
            return;
        }

        var bank_addr = $("#bankaddress").val().trim();
        if (!bank_addr) {
            alert2("请输入开户网点", true);
            return;
        }
        bank = {
            bank: $("#bankCode").val(),
            account: bank_account,
            addr: bank_addr,
        };
        startLoading();
        CMD.modify_user_info(undefined, {
            bank: JSON.stringify(bank),
        }, function() {
            stopLoading();
            context.center.menu = 'withdraw';
            context.user.user.bank = JSON.stringify(bank);
            switch_page("center/money", $(".content-wrap"));
            alert2("绑定银行卡成功", false);
        });
    });

    // 申请提款的处理
    $("#withdraw-form .c-button").click(function() {
        var amount = $("#drawamount").val();
        if (amount == '') return;
        amount = parseInt(amount);
        if (amount < 100) {
            alert2("金额不能少于100", true);
            return;
        }
        if (amount > 100000) {
            alert2("金额不能高于100000", true);
            return;
        }

        if (context.user.dw > 0) {
            alert2("您的投注额度尚未达到要求，暂时无法取款", true);
            return;
        }
        if (context.user.balance < amount * 100) {
            alert2("余额不足", true);
            return;
        }

        // 检测取款密码
        var p = $("#drawcode1").val().trim();
        if (p != context.user.user.tpassword && context.user.user.tpassword) {
            alert2("取款密码错误", true);
            return;
        }

        var memo = {
            bank: bank.bank,
            addr: bank.addr,
            account: bank.account,
        }
        startLoading();
        CMD.withdraw(context.user.user.id, "", amount*100, memo, function() {
            stopLoading();
            alert2("申请成功，请等待审核", false);
            ME.query(function() {
                // 跳转到个人中心
                context.center.menu = 'notice';
                switch_page("center/notice", $(".content-wrap"));
            });
            
        }, function(message) {
            stopLoading();
            alert2("提交失败，错误原因：" + message, true);
            ME.query();
        });
    });
</script>